package person.bluext.singlegateway.frame.utils;

import lombok.extern.slf4j.Slf4j;
import person.bluext.singlegateway.common.utils.ProjectStringUtils;
import person.bluext.singlegateway.frame.model.HttpInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Enumeration;

/**
 * Description : Utils to deal with http info
 *
 * @author : bluext
 */
@Slf4j
public class HttpInfoUtils {

    /**
     * 获取请求头信息
     *
     * @param request 请求对象
     * @return 请求头信息
     */
    public static String getStringHeaders(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            sb.append("[").append(headerName).append(": ").append(headerValue).append("] ");
        }
        return sb.toString();
    }

    /**
     * 获取响应头信息
     *
     * @param response 响应对象
     * @return 响应头信息
     */
    public static String getStringHeaders(HttpServletResponse response) {
        StringBuilder sb = new StringBuilder();
        Collection<String> headerNames = response.getHeaderNames();
        for (Object headerName1 : headerNames) {
            String headerName = String.valueOf(headerName1);
            String headerValue = response.getHeader(headerName);
            sb.append("[").append(headerName).append(": ").append(headerValue).append("] ");
        }
        return sb.toString();
    }

    /**
     * 读取请求体中请求参数
     *
     * @param request 请求对象
     * @return 请求参数信息
     */
    public static String readPayload(HttpServletRequest request) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            StringBuilder sb = new StringBuilder();
            int length;
            char[] temp = new char[1024];
            while ((length = reader.read(temp)) != -1) {
                sb.append(temp, 0, length);
            }
            return sb.toString();
        } catch (Exception e) {
            log.error("Fail to Read Json Payload , Exception:", e);
        }
        return "";
    }

    /**
     * 获取请求者ip地址等信息
     *
     * @param request 请求对象
     * @return 请求方ip地址信息
     */
    public static HttpInfo getReqIpInfo(HttpServletRequest request) {
        String x_real_ip = request.getHeader("x-real-ip");
        String x_forwarded_for = request.getHeader("x-forwarded-for");
        String remote_host = request.getHeader("remote-host");
        HttpInfo resultDto = new HttpInfo();
        if (!ProjectStringUtils.isBlank(x_real_ip)) {
            resultDto.setX_real_ip(x_real_ip);
            resultDto.setX_forwarded_for(x_forwarded_for);
            resultDto.setRemote_host(remote_host);
            resultDto.setDispatched(true);
        } else {
            String remoteAddress = request.getRemoteAddr();
            String remoteHost = request.getRemoteHost();
            Integer remotePort = request.getRemotePort();
            resultDto.setRemoteAddress(remoteAddress);
            resultDto.setRemoteHost(remoteHost);
            resultDto.setRemotePort(remotePort);
            resultDto.setDispatched(false);
        }
        return resultDto;
    }
}
